<?php
require_once '../setup.inc.php';

//Check if user is logged in or show login form
session_start();
if (empty($_SESSION['EVE API Reg Admin'])) {
  if (empty($_POST['password']) or $_POST['password'] != $config['Admin Password']) {
    echo <<<EOL
<!DOCTYPE HTML>
<html>
  <head>
    <meta http-equiv="content-type" content="text/html; charset=utf-8"/>
    <title>
      Password Required
    </title>
  </head>
  <body>
    <form method="post" action="{$_SERVER['REQUEST_URI']}">
      <p>
        Password:
        <input type="password" name="password" />
        <input type="submit" value="Log in"/>
      </p>
    </form>
  </body>
</html>
EOL;
    die;
  }
  else {
    $_SESSION['EVE API Reg Admin'] = TRUE;
    header('Location: ' . $_SERVER['REQUEST_URI']);
    die;
  }
}

connect_forums_db();

echo <<<EOL
<!DOCTYPE HTML>
<html>
  <head>
    <meta http-equiv="content-type" content="text/html; charset=utf-8"/>
    <title>
      Registration List
    </title>
    <style type="text/css">
html {
  background-color: #000;
  color: #fff;
  font-size: .75em;
  font-family: Arial, Helvetica, sans-serif;
}
table {
  border-collapse: collapse;
  margin: 1em auto;
  border: 1px solid #777;
}
td, th {
  padding: 5px;
  border-style: none;
}
tr.odd {
  background-color: #555;
}
tr.even {
  background-color: #222;
}
a {
  color: #fff;
}
input {
  font-size: 12px;
  background-color: #3b3b3b;
  color: #fff;
  border: 1px solid #000;
}
input.username {
  width: 12em;
}
input.eve_user_id {
  width: 4.5em;
}
input.api_key {
  width: 19em;
  font-family: 'Courier New', monospace;
}
input.ts_uid {
  width: 17em;
  font-family: 'Courier New', monospace;
}
h1 {
  text-align: center;
  font-size: 1.3em;
}
textarea {
  margin: 0px auto;
  display: block;
  width: 988px;
}
    </style>
  </head>
  <body>
EOL;

//Determine sort order
if (!isset($_GET['order'])) {
  $_GET['order'] = NULL;
}
switch ($_GET['order']) {
  case 'reg-date':
    $order_clause = '`Forums Reg Date`';
    break;
  case 'username':
    $order_clause = '`Username` COLLATE utf8_unicode_ci';
    break;
  case 'key-id':
    $order_clause = '`Key ID`';
    break;
  case 'ts-uid':
    $order_clause = '`TS UID`';
    break;
  case 'cached-until':
    $order_clause = '`Cached Until`';
    break;
  default:
    $order_clause = '`Forums Reg Date`';
}
if ((isset($_GET['desc']) and $_GET['desc'] == 1) or ($order_clause == '`Forums Reg Date`' and !isset($_GET['desc']))) {
  $order_clause .= ' DESC';
}

//Get users from forums DB
$query = <<<EOL
SELECT
  user_regdate AS `Forums Reg Date`,
  username AS `Username`,
  `Key ID`,
  `Key Ver Code`,
  `TS UID`,
  `Cached Until`
FROM `EVE Registration`
JOIN {$config['Forums DB']['Table Prefix']}users ON {$config['Forums DB']['Table Prefix']}users.user_id = `EVE Registration`.User
ORDER BY {$order_clause}
EOL;
$result = mysql_query($query) or die('Error getting user information.');


//Output registration list
$current_time = date('Y-m-d H:i:s T');
$time_zone = date('T');
echo "\n" . <<<EOL
    <h1>
      Registration List
    </h1>
    <p style="text-align: center;">
      Current Time: {$current_time} - <a href="../readme.html">View Documentation</a>
    </p>
    <table>
      <tr class="odd">
        <th>
          Forums Reg. Date
          <br />
          <a href="./?order=reg-date&desc=0">Sort</a> - <a href="./">Reverse</a>
        </th>
        <th>
          Username
          <br />
          <a href="./?order=username">Sort</a> - <a href="./?order=username&desc=1">Reverse</a>
        </th>
        <th>
          Key ID
          <br />
          <a href="./?order=key-id">Sort</a> - <a href="./?order=key-id&desc=1">Reverse</a>
        </th>
        <th>
          Key Ver. Code
        </th>
        <th>
          Teamspeak Unique ID
          <br />
          <a href="./?order=ts-uid">Sort</a> - <a href="./?order=ts-uid&desc=1">Reverse</a>
        </th>
        <th>
          Cached Until ({$time_zone})
          <br />
          <a href="./?order=cached-until">Sort</a> - <a href="./?order=cached-until&desc=1">Reverse</a>
        </th>
      </tr>
EOL;
$class = 'odd';
while ($user = mysql_fetch_assoc($result)) {
  $user['Forums Reg Date'] = date('Y-m-d H:i:s', $user['Forums Reg Date']);
  if ($class == 'odd') {
    $class = 'even';
  }
  else {
    $class = 'odd';
  }
  echo "\n" . <<<EOL
        <tr class="{$class}">
          <td>
            {$user['Forums Reg Date']}
          </td>
          <td>
            {$user['Username']}
          </td>
          <td>
            {$user['Key ID']}
            <a href="https://api.eveonline.com/account/APIKeyInfo.xml.aspx?keyID={$user['Key ID']}&amp;vCode={$user['Key Ver Code']}" target="_blank">Check API</a>
          </td>
          <td>
            <input type="text" value="{$user['Key Ver Code']}" style="width: 17em;" />
          </td>
          <td>
            {$user['TS UID']}
          </td>
          <td>
            {$user['Cached Until']}
          </td>
        </tr>
EOL;
}
echo "\n" . <<<EOL
    </table>
    <h1>
      Registration Errors (Last 10)
    </h1>
    <p style="text-align: center;">
      Current Time: {$current_time}
    </p>
    <textarea rows="17" cols="120">
EOL;
passthru('tail -n 10 ../errors.txt');
echo <<<EOL
</textarea>
  </body>
</html>
EOL;
?>